
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
    //获取
    async index() {
        const { ctx } = this;
        //获取团购信息
        const data = await this.app.mysql.query(`SELECT * FROM grouplist order by id desc`);
        ctx.body = {
            code: 200,
            data: data
        };
    }
    //删除
    async delGroup() {
        const { ctx } = this;
        const id = JSON.parse(ctx.query.params).id;
        console.log(id);
        try {
            await this.app.mysql.delete('grouplist', { id })
        } catch (err) {
            ctx.body = {
                code: 500,
                msg: '操作失败'
            };
        }
        ctx.body = {
            code: 200,
            msg: '删除成功'
        };
    }
    //获取详情
    async detailGroup() {
        const { ctx } = this;
        const { id } = ctx.query
        //获取团购信息
        const data = await this.app.mysql.get('grouplist', { id });
        ctx.body = {
            code: 200,
            data: data
        };
    }
    //改变状态
    async setGroup() {
        const { ctx } = this;
        const { flag, id } = ctx.request.body
        //获取团购信息
        const data = await this.app.mysql.update('grouplist', { flag: flag + '' }, {
            where: {
                id
            }
        });
        ctx.body = {
            code: 200,
            data: data,
            msg: '修改成功'
        };
    }
    //添加团购
    async addGroup() {
        const { ctx } = this;
        const { form } = ctx.request.body
        const data = await this.app.mysql.insert('grouplist', {
            name: form.name, img: form.img, address: form.address,
            initiator: '李俊', Update_time: form.update,
            status: '未开团', state: 0, num: 0, flag: 'false', isPay: 'false'
        });
        ctx.body = {
            code: 200,
            data
        };
    }
}

module.exports = HomeController;